using System;
using System.Text.RegularExpressions;
using Habanero.Base;
using Habanero.Base.Exceptions;
using Habanero.BO;

namespace FireStarterModeller.BO
{
    /// <summary>
    /// The generated code will not work property in the case where a class
    /// e.g. Solution is part of the namespace Solution.BO the generics etc 
    /// will not be able to differentiate between the classname and the namespace.
    /// </summary>
    public class DMClassNameNotEqualToAssemblyName : IBusinessObjectRule
    {
       
        private DMClass _dmClass;
        private string _className;

        public DMClassNameNotEqualToAssemblyName()
        {
//            if (dmAssembly == null) throw new ArgumentNullException("dmAssembly");
//            _dmAssembly = dmAssembly;
        }

        #region Implementation of IBusinessObjectRule

        /// <summary>
        /// Indicates whether the property value is valid against the rules
        /// </summary>
        /// <returns>
        /// Returns true if valid
        /// </returns>
        public bool IsValid()
        {
            throw new HabaneroApplicationException("The IsValid is no longer Valid you should use the IsValid(bo) method");
        }

        //public bool IsValid(IBusinessObject bo)
        //{
        //    _dmClass = (DMClass) bo;
        //    if (String.IsNullOrEmpty(_dmClass.ClassNameBO)) return true;
        //    //return Regex.IsMatch(_dmClass.ClassNameBO, "^[a-zA-Z]{1}([a-zA-Z0-9_]+)$");
        //    return Regex.IsMatch(_dmClass.ClassNameBO, "^[a-zA-Z0-9_]*$");
        //}

        public bool IsValid(IBusinessObject bo)
        {
            _dmClass = (DMClass)bo;
            string previousClassName = "";
           
                if (String.IsNullOrEmpty(_dmClass.ClassNameBO)) return true ;
                if (_dmClass.ClassNameBO.Equals(previousClassName))
                {
                    _className = previousClassName;
                    return false;
                }
                if (_dmClass.Assembly == null) return true;
                if (_dmClass.ClassNameBO.Equals(_dmClass.Assembly.AssemblyName))
                {
                    _className = previousClassName;
                    return false;
                }
                previousClassName = _dmClass.ClassNameBO;
      
            return true;
        }
        /// <summary>
        /// Returns the rule name
        /// </summary>
        public string Name
        {
            get { return "DMClassNameNotEqualToAssemblyName"; }
        }

        /// <summary>
        /// Returns the error message for if the rule fails.
        /// </summary>
        public string Message
        {
            get
            {
                return "The Entity/Class '" + _dmClass.ToString() + "' cannot have the same name as its Assembly/Namespace '";
            }
        }
        /// <summary>
        /// The <see cref="P:Habanero.Base.IBusinessObjectRule.ErrorLevel" /> for this BusinessObjectRule e.g. Warning, Error. 
        /// </summary>
        public ErrorLevel ErrorLevel
        {
            get { return Habanero.Base.ErrorLevel.Error; }
        }

        #endregion
    }
}


